package com.hnit.contrllers;

import com.hnit.config.RabbitMQConfig;
import com.hnit.service.VectorStoreService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


//将耗时的向量化操作移到消息队列中，主线程只负责将任务发送到队列

@RestController
@RequestMapping("/api/vector")
@CrossOrigin(origins = "*")
public class VectorStoreController {

    private final VectorStoreService vectorStoreService;
    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public VectorStoreController(VectorStoreService vectorStoreService, RabbitTemplate rabbitTemplate) {
        this.vectorStoreService = vectorStoreService;
        this.rabbitTemplate = rabbitTemplate;
    }

    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addJsonData(@RequestBody Map<String, Object> jsonData) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 生成任务ID
            String taskId = UUID.randomUUID().toString();
            jsonData.put("taskId", taskId);

            // 发送任务到RabbitMQ
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, jsonData);

            response.put("success", true);
            response.put("message", "任务已提交到队列");
            response.put("taskId", taskId);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "任务提交失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.internalServerError().body(response);
        }
    }

    @PostMapping("/batch")
    public ResponseEntity<Map<String, Object>> addJsonDataBatch(@RequestBody List<Map<String, Object>> jsonDataList) {
        Map<String, Object> response = new HashMap<>();
        try {
            String taskId = UUID.randomUUID().toString();
            Map<String, Object> taskData = new HashMap<>();
            taskData.put("taskId", taskId);
            taskData.put("dataList", jsonDataList);

            // 发送批量任务到RabbitMQ
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, taskData);

            response.put("success", true);
            response.put("message", "批量任务已提交到队列");
            response.put("taskId", taskId);
            response.put("totalRecords", jsonDataList.size());

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "批量任务提交失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    @PostMapping("/text")
    public ResponseEntity<Map<String, Object>> addTextData(@RequestBody Map<String, String> textData) {
        Map<String, Object> response = new HashMap<>();
        try {
            String content = textData.get("content");
            if (content == null || content.trim().isEmpty()) {
                response.put("success", false);
                response.put("message", "content字段不能为空");
                return ResponseEntity.badRequest().body(response);
            }

            String taskId = UUID.randomUUID().toString();
            Map<String, Object> taskData = new HashMap<>();
            taskData.put("taskId", taskId);
            taskData.put("content", content);

            // 发送文本任务到RabbitMQ
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, taskData);

            response.put("success", true);
            response.put("message", "文本任务已提交到队列");
            response.put("taskId", taskId);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "文本任务提交失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> healthCheck() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "ok");
        response.put("message", "Vector store service is running");
        return ResponseEntity.ok(response);
    }
}